package com.supinfo.suptracking.servlet;

import com.supinfo.suptracking.entity.Zone;
import com.supinfo.suptracking.service.ZoneService;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Fabien, Jérémy, Ludovic, Brice
 */

@WebServlet(name = "EditZoneServlet", urlPatterns = {"/auth/editZones"})
public class EditZoneServlet extends HttpServlet {

    @EJB
    private ZoneService zoneServ;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
      doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        Date dateLow = null;
        Date dateHigh = null;
        String zoneIdTemp = request.getParameter("zoneIdEdit");
        String zoneName = request.getParameter("zoneNameEdit");
        String zoneLatitude = request.getParameter("zoneLatitudeEdit");
        String zoneLongitude = request.getParameter("zoneLongitudeEdit");
        String zoneRadiusMeter = request.getParameter("zoneRadiusMeterEdit");
        String zoneLowTimeStamp = request.getParameter("zoneLowTimeStampEdit");
        String zoneHighTimeStamp = request.getParameter("zoneHighTimeStampEdit");
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            dateLow = sdf.parse(zoneLowTimeStamp);
        } catch (ParseException ex) {
            Logger.getLogger(AddCarServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            dateHigh = sdf.parse(zoneHighTimeStamp);
        } catch (ParseException ex) {
            Logger.getLogger(AddCarServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        // Transform string in long
        Long zoneId = Long.valueOf(zoneIdTemp);
        
        // Get zone and update it
        Zone zoneEdited = zoneServ.processGetZoneById(zoneId);
        
        zoneEdited.setLatitude(zoneLatitude);
        zoneEdited.setLongitude(zoneLongitude);
        zoneEdited.setRadiusMeter(zoneRadiusMeter);
        zoneEdited.setName(zoneName);
        zoneEdited.setLowTimeStamp(dateLow);
        zoneEdited.setHighTimestamp(dateHigh);
        
        // Update in base
        zoneServ.processUpdateZone(zoneEdited);
        
        // Notification of user and redirection
        request.getSession().setAttribute("event", "1");
        request.getSession().setAttribute("message", "Zone modified");
        response.sendRedirect(getServletContext().getContextPath()+"/auth/zones");	
    }
}
